package com.example.module.shoppingCart.service.impl;


import com.example.common.unit.ConvertUnit;
import com.example.common.unit.GetUserInfoBySessionUnit;
import com.example.module.goods.form.GetGoodsForm;
import com.example.module.goods.mapper.GoodsMapper;
import com.example.module.goods.vo.GetGoodsVO;
import com.example.module.shoppingCart.DO.CartUpdateDO;
import com.example.module.shoppingCart.DO.ShoppingCartAddDO;
import com.example.module.shoppingCart.form.CartUpdateForm;
import com.example.module.shoppingCart.form.GetShopingCartForm;
import com.example.module.shoppingCart.form.ShoppingCartAddForm;
import com.example.module.shoppingCart.form.ShoppingCartDeleteForm;
import com.example.module.shoppingCart.mapper.ShoppingCartMapper;
import com.example.module.shoppingCart.service.ShoppingCartService;
import com.example.module.shoppingCart.vo.GetShoppingCartVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ghy
 * @date 2025/08/28
 * @description 购物车服务实现类
 */
@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private ConvertUnit convertUnit;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private GetUserInfoBySessionUnit getUserInfoBySessionUnit;

    /*
     * 添加商品至购物车
     *
     * @param shoppingCartAddForm
     * @return void
     * */
    @Override
    public void addShoppingCart(ShoppingCartAddForm shoppingCartAddForm) {
        // 判断是否存在该商品
        GetGoodsForm getGoodsForm = new GetGoodsForm();
        getGoodsForm.setGoodsId(shoppingCartAddForm.getGoodsId());
        List<GetGoodsVO> goods = goodsMapper.getGoods(getGoodsForm);
        if (goods.size() == 0) {
            throw new RuntimeException("加入购物车失败：商品不存在");
        }
        //判断是否已经添加购物车
        if(shoppingCartMapper.getCartByGoodsId(shoppingCartAddForm.getGoodsId()) != null){
            throw new RuntimeException("加入购物车失败：商品已添加至购物车");
        }
        // 添加到数据库
        ShoppingCartAddDO shoppingCartAddDO = convertUnit.FormToDO(shoppingCartAddForm, ShoppingCartAddDO.class);
        //从session中获取用户Id并添加到DO中
        shoppingCartAddDO.setUserId(getUserInfoBySessionUnit.getUserId());
        shoppingCartMapper.addShoppingCart(shoppingCartAddDO);
    }

    /*
     *  根据商品Id查询对应收藏
     *
     * @param getShopingCartForm
     * @return getShoppingCartVO
     * */
    @Override
    public GetShoppingCartVO getShoppingCartByGoodsId(GetShopingCartForm getShopingCartForm) {
        Long goodsId = getShopingCartForm.getGoodsId();
        GetShoppingCartVO result = shoppingCartMapper.getCartByGoodsId(goodsId);
        return result;
    }

    /*
     * 从购物车中移除商品
     *
     * @param ShoppingCartDeleteForm
     * @return void
     * */
    @Override
    public void deleteShoppingCart(ShoppingCartDeleteForm form) {
        //判断购物车中是否存在对应记录
        GetShoppingCartVO shoppingCart = shoppingCartMapper.getCartByCartId(form.getShoppingCartId());
        if (shoppingCart == null) {
            throw new RuntimeException("删除购物车失败：购物车中不存在该商品");
        }
        // 获取购物车Id
        Long shoppingCartId = form.getShoppingCartId();
        // 删除购物车
        shoppingCartMapper.deleteShoppingCart(shoppingCartId);
    }

    /*
     * 批量修改购物车中的商品数量
     *
     * @param List<CartUpdateForm>
     * @return void
     * */
    @Override
    public void updateCart(List<CartUpdateForm> formList) {
        // 遍历表单列表，逐个更新购物车记录
        for (CartUpdateForm cartUpdateForm : formList) {
            // 检查购物车记录是否存在
            GetShoppingCartVO shoppingCart = shoppingCartMapper.getCartByCartId(cartUpdateForm.getShoppingCartId());
            if (shoppingCart == null) {
                throw new RuntimeException("更新购物车失败：购物车中不存在ID为 " + cartUpdateForm.getShoppingCartId() + " 的商品");
            }
            // 将Form转换为DO并更新数据库
            CartUpdateDO cartUpdateDO = convertUnit.FormToDO(cartUpdateForm, CartUpdateDO.class);
            shoppingCartMapper.updateCart(cartUpdateDO);
        }
    }

}
